﻿package Classes{

	import Classes.Events.*;
	import flash.events.*;
	import Classes.BaseProperties;
	import flash.utils.*;
	import flash.display.*;
	
	import com.greensock.*;
	import com.greensock.easing.*;

	public class GameController extends EventDispatcher {

		/* Level object */
		private var currentLevel:Level;
		private var levelCount:Number;
		private var aantalLevel:Number;
		
		private var gameOver:Function;

		/* Constructor */
		public function GameController(s:Function) {
			
			levelCount=0;
			aantalLevel=1;
			
			this.gameOver = s;
			
		}

		/* Start Methode */
		public function run() {

			levelEnd(null);

		}
		
		public function startRun(eventArgs:TimerEvent){
			
			this.currentLevel.run();
			
		}

		public function levelEnd(e:Event) {
			
			if (this.currentLevel){
				
				this.currentLevel.quitLevel();
				
				var myTimer:Timer = new Timer(1500, 1);
				myTimer.addEventListener("timer", effStart);
				
				myTimer.start();

				
			}else{
			
				effStart(null);
			
			}
			
		}
		
		public function effStart(eventArgs:TimerEvent){
				
			var end:Boolean=false;
				
			switch(levelCount){
				
				case 0:
				this.currentLevel=new Level(this,8,20);
				this.currentLevel.startUp("KAYAM", new TentLevel1());
				break;
				
				case 1:
				this.currentLevel=new Level(this,6,35);
				this.currentLevel.startUp("KRING", new TentLevel2());
				break;
				
				case 2:
				this.currentLevel=new Level(this,4,40);
				this.currentLevel.startUp("PALACE", new TentLevel3());
				break;
				
				case 3:
				this.currentLevel=new Level(this,2,55);
				this.currentLevel.startUp("CLUB", new TentLevel4());
				break;
				
				default:
				end=true;
				gameOver();
				break;
				
			}
			
			if (!end){
			
				levelCount++;
			
				var myTimer:Timer = new Timer(1500, 1);
				myTimer.addEventListener("timer", startRun);
				
				myTimer.start();
				
				this.currentLevel.addEventListener("LevelEnded", levelEnd);
			
			}

		}

	}

}